﻿/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

package com.filipesilvestrim.status.state
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	final public class BitState
	{
		// ___________________________________________________________________ CONSTANTS
		static public const IDLE	: String 		= "idle";

		// ___________________________________________________________________ CLASS PROPERTIES
		private var _actualState 		: int;
		private var _linkLastState 		: String;
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		private var _dicStates			: Dictionary;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		
		// ___________________________________________________________________ CONSTRUCTOR
		public function BitState(initialStates : Array = null)
		{
			_dicStates				= new Dictionary(true);
			_dicStates[IDLE] 		= 1;
			_linkLastState			= IDLE;
			
			for each ( var state : String in initialStates)
			{
				addState(state);
				setState(state);
			}
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		/**
		 * ///////////////////////////////////////////
		 * BYTES STATES 
		 * ///////////////////////////////////////////
		 * 
		 * TODO - filipe - colocar remove state
		 */
		public function addState ( name : String ) : void
		{
			_dicStates[name]	= Math.pow(_dicStates[_linkLastState], 2);
			_linkLastState 		= name;
		}
		
		public function getStateValue ( name : String ) : int
		{
			return _dicStates[name];
		}
		
		public function isStateSet ( name : String ) : Boolean
		{
			return (getStateValue(name) & _actualState) != 0;
		}
		
		public function setState ( name : String ) : void
		{
			if (!isStateSet(name))
			{
				_actualState |= getStateValue(name);
			}
			
//			callBitChange();
		}
		
		public function unsetState ( name : String ) : void
		{
			if (isStateSet(name))
			{
				_actualState &= ~getStateValue(name);
			}
			
//			callBitChange();
		}
		
		public function resetState () : void
		{
			_actualState = _dicStates[IDLE];
			
//			callBitChange();
		}
		
		public function toString () : String
		{
			var str : String = "[Filipe Silvestrim] :: BitState[" + this + "].states = ";
			
			for each ( var st : String in _dicStates)
			{
				if (isStateSet(st))
					str.concat(st + ", ");
			}
			
			return str; 
		}
		// ___________________________________________________________________ PRIVATE METHODS
		
		// ___________________________________________________________________ EVENTS
	}
}

